--[[
FilePath: \Lua\View\UISoloBattle\UISoloBattleRank.lua
1v1排行榜
--]]

require 'View/UIBase/UIBase'

UISoloBattleRank = UIBase.New(PanelNames.UISoloBattleRank)
local _M = UISoloBattleRank

_M.hasInit = false

function _M.Init(func)
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self.btnClose = self:GetChild("Btn_Back");
        self.title = self:GetChild("Com_ThreeBg"):GetChild("title");
        --self.btnRankTop1 = self:GetChild("Btn_RankTop1");
        self.listRank = self:GetChild("Com_RankList"):GetChild("List_Rank");
        --self.textPlayer = self:GetChild("textPlayer");
        self.myRankNode = self:GetChild("Com_MyRank");

        self.listRank.itemRenderer = self.refreshItem;
        self.listRank:SetVirtual();

        _M.hasInit = true

        self:addEvent()
    end
    RealManWarHandler.sendReqRealManFuckRankListMessage();
    self:initView();
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.Fresh_RealManRank,self.realManRank);
    self:RegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeRealManRank);
    self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);
    self.btnClose.onClick:Add(self.onClosePanel,self);
end

function _M:removeEvent()
    self.btnClose.onClick:Remove(self.onClosePanel);
    self:UnRegisterEvent(Config.EventType.Fresh_RealManRank,self.realManRank);
    self:UnRegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeRealManRank);
    self:UnRegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);
end

function _M:GotOtherShowInfo(info)
    showUI(PanelNames.UIPlayerShow, info,nil,false,function()
        UIBottom.enterAdventure()
        logicMgr.RealManWarManager.setFightResult(nil);
        logicMgr.RealManWarManager.setEnterBattle(true);
        logicMgr.RealManWarManager.OpenSoloBattle();
        showUI(PanelNames.UISoloBattleRank);
    end)
end

function _M:initView()
    --self.title.text = getLanguage("深渊排行榜");
    --self.textPlayer.text = getLanguage("我的排名");
end

function _M:closeRealManRank()
    self:onClosePanel();
end

function _M:realManRank()
    self.rankList = {};
    self.myRank = nil;
    local rankInfo = logicMgr.RealManWarManager.getRealManFuckRankList();
    if rankInfo ~= nil then
        self.rankList = rankInfo.list;
        self.myRank = rankInfo.myRank;
    end
    if self.rankList ~= nil and #self.rankList > 0 then
        table.sort(self.rankList, function(a, b)
            return a.rank < b.rank;
        end)
    end
    self:updateBattleRank();
end

function _M.refreshItem(index, itemObj)
    _M:updateItem(itemObj, _M.rankList[index + 1]);
end

function _M:updateItem(itemObj, data, isMy)
    local otherRank = itemObj:GetChild("Text_OtherRank");
    local myRank = itemObj:GetChild("Text_MyRank");
    local btnHead = itemObj:GetChild("Btn_Head");
    local headIcon = btnHead:GetChild("head"):GetChild("HeadIcon");
    local headFrame = btnHead:GetChild("frame");
    local firstCtrl = btnHead:GetController("first");
    local playerName = itemObj:GetChild("Text_PlayerName");
    local textLevel = itemObj:GetChild("Text_Level");
    --local textTime = itemObj:GetChild("Text_Time");
    local rankCtrl = itemObj:GetController("rank");
    local emptyCtrl = itemObj:GetController("empty");
    local rankingCtrl = itemObj:GetController("ranking");
    firstCtrl.selectedIndex = 0;
    btnHead.onClick:Clear();
    if data == nil then
        if isMy ~= nil and isMy then
            emptyCtrl.selectedIndex = 0;
            rankCtrl.selectedIndex = 1;
            rankingCtrl.selectedIndex = 3;
            playerName.text = logicMgr.HeroManager.GetRoleName();
            self:SetPlayerIcon(headIcon, logicMgr.HeroManager.GetRoleHeadID(), headFrame, logicMgr.HeroManager.GetRoleHeadFrame());
            local abyssCfg = G.dataTable["t_abyss"]["1"];
            if abyssCfg ~= nil then
                textLevel.text = string.format(getLanguage("SoloBattle_RankLevel"), string.format(getLanguage("SoloBattle_Level"), getLanguage("SoloBattle_difficulty0" .. abyssCfg.f_Rank), abyssCfg.f_RankLevel));
            else
                textLevel.text = string.format(getLanguage("SoloBattle_RankLevel"), "--");
            end
            myRank.text = getLanguage("NotInRank");
            otherRank.text = getLanguage("NotInRank");
        else
            rankCtrl.selectedIndex = 0;
            emptyCtrl.selectedIndex = 1;
        end
    else
        emptyCtrl.selectedIndex = 0;
        rankCtrl.selectedIndex = 0;
        self:SetPlayerIcon(headIcon, data.headIcon, headFrame, data.headFrame);
        playerName.text = data.name;
        local abyssCfg = G.dataTable["t_abyss"][tostring(data.score)];
        if abyssCfg ~= nil then
            textLevel.text = string.format(getLanguage("SoloBattle_RankLevel"), string.format(getLanguage("SoloBattle_Level"), getLanguage("SoloBattle_difficulty0" .. abyssCfg.f_Rank), abyssCfg.f_RankLevel));
        else
            textLevel.text = string.format(getLanguage("SoloBattle_RankLevel"), "--");
        end

        if data.rank > 3 then
            rankingCtrl.selectedIndex = 3;
        else
            if data.rank == 1 then
                firstCtrl.selectedIndex = 1;
                if self.effect_first == nil then
                    self.effect_first = Util.SetSkeletonAnimMatrialStencil(btnHead, 3)
                end
            end
            rankingCtrl.selectedIndex = (data.rank - 1);
        end
        if isMy ~= nil and isMy then
            rankCtrl.selectedIndex = 1;
            if data.rank == 0 or data.rank > 50 then
                otherRank.text = getLanguage("NotInRank");
            else
                otherRank.text = tostring(data.rank);
            end
            --myRank.text = tostring(data.rank);
        else
            otherRank.text = tostring(data.rank);
            btnHead.onClick:Add(function()
                if tostring(data.playerId) ~= logicMgr.HeroManager.GetRoleId() then
                    roleInfoHandler.sendreqOtherRoleInfo(data.playerId);
                end
            end);
        end

    end
end

function _M:resetEffect()
    if self.effect_first then
        for i=0, self.effect_first.Length - 1 do
            self.effect_first:GetValue(i):SetFloat("_StencilComp", 8)
        end
        self.effect_first = nil
    end
end

function _M:updateTop(itemObj, data)
    local btnHead = itemObj:GetChild("Btn_Head");
    local headIcon = btnHead:GetChild("head"):GetChild("HeadIcon");
    local headFrame = btnHead:GetChild("frame");
    local playerName = itemObj:GetChild("Text_PlayerName");
    --local textLevel = itemObj:GetChild("Text_Level");
    local textTime = itemObj:GetChild("Text_WinRateNum");
    local rankPointNum = itemObj:GetChild("Text_RankPointNum");
    local rankCtrl = itemObj:GetController("Player");

    itemObj.onClick:Clear();
    if data ~= nil then
        rankCtrl.selectedIndex = 0;
        self:SetPlayerIcon(headIcon, data.headIcon, headFrame, data.headFrame);
        playerName.text = data.name;
        local abyssCfg = G.dataTable["t_abyss"][tostring(data.score)];
        if abyssCfg ~= nil then
            rankPointNum.text = string.format(getLanguage("SoloBattle_Level"), getLanguage("SoloBattle_difficulty0"..abyssCfg.f_Rank),abyssCfg.f_RankLevel);
        else
            rankPointNum.text = "";
        end
        --textLevel.text = data.level;
        itemObj.onClick:Add(function()
            if tostring(data.playerId) ~= logicMgr.HeroManager.GetRoleId() then
                roleInfoHandler.sendreqOtherRoleInfo(data.playerId);
            end
        end);
    else
        rankCtrl.selectedIndex = 1;
    end
end

-- 显示
function _M:OnEnable()
    --TrialHandel.SendTrialsWorldRank(); 获取排行榜
end

function _M:updateBattleRank()
    self:updateItem(self.myRankNode, self.myRank, true);
    self.listRank.numItems = 50;
    HelperFunc.ListTranstionPlay(self.listRank);
    --self:updateTop(self.btnRankTop1, self.rankList[1]);
end

function _M:onClosePanel()
    closeUI(PanelNames.UISoloBattleRank);
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self:resetEffect();
    self.hasInit = false
end

return _M;